﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using TravelWallet.Types;
namespace TravelWallet
{
    public partial class AddCustomProgramPage : PhoneApplicationPage
    {
        private ProgramCategory category;

        public static string lastProgramNameCreated;
        public static string lastNameCreated;

        public AddCustomProgramPage()
        {
            InitializeComponent();
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            //need to process the new program information
            string name = this.tb_Name.Text;
            string programName = this.tb_ProgramName.Text;

            if (category != ProgramCategory.Hotels && string.IsNullOrEmpty(name))
            {
                MessageBox.Show("Name cannot be empty");
            }
            else if (string.IsNullOrEmpty(programName))
            {
                MessageBox.Show("Program name cannot be empty");
            }
            else
            {
                //check to make sure that this Name isnt already in the ViewModel
                if (!App.ViewModel.DoesNameExist(name) || category == ProgramCategory.Hotels)
                {
                    //add the program to the viewmodel
                    Program newProgram = App.ViewModel.AddProgram(name, programName, category);
                    AddCustomProgramPage.lastProgramNameCreated = newProgram.ProgramName;
                    AddCustomProgramPage.lastNameCreated = newProgram.Name;
                    //go back
                    NavigationService.GoBack();
                }
                else
                {
                    //name already exists, error
                    string strCategory = this.category.ToString();
                    MessageBox.Show(string.Format("A {0} already exists with the name {1}. Please try again.", strCategory, name));
                }
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            AddCustomProgramPage.lastProgramNameCreated = null;
            AddCustomProgramPage.lastNameCreated = null;
            NavigationService.GoBack();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            string strCategory = null;

            if (NavigationContext.QueryString.TryGetValue("category", out strCategory))
            {
                this.category = (ProgramCategory)Enum.Parse(typeof(ProgramCategory), strCategory, true);
                this.lp_Category.SelectionChanged -= lp_Category_SelectionChanged;
            }

        }

        private void lp_Category_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.category = GetSelectedCategory();
            if (this.category == ProgramCategory.Hotels)
            {
                tb_Name.Visibility = System.Windows.Visibility.Collapsed;
                lbl_Name.Visibility = System.Windows.Visibility.Collapsed;
                tb_Name.Text = string.Empty;
            }
            else {
                tb_Name.Visibility = System.Windows.Visibility.Visible;
                lbl_Name.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private ProgramCategory GetSelectedCategory()
        {
            ProgramCategory category;
            string newCategory = lp_Category.SelectedItem.ToString();
            newCategory = newCategory.Replace(" ", "");


            category = (ProgramCategory)Enum.Parse(typeof(ProgramCategory), newCategory, true);
            return category;
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.lp_Category.DataContext = App.ViewModel.CategoryNames;
            this.lp_Category.ItemsSource = App.ViewModel.CategoryNames;

            if (this.category == ProgramCategory.Hotels)
            {
                this.tb_Name.Visibility = System.Windows.Visibility.Collapsed;
                this.lbl_Name.Visibility = System.Windows.Visibility.Collapsed;
                tb_Name.Text = string.Empty;
            }
            else
            {
                this.tb_Name.Visibility = System.Windows.Visibility.Visible;
                this.lbl_Name.Visibility = System.Windows.Visibility.Visible;
            }

            if (this.category == ProgramCategory.Hotels || this.category == ProgramCategory.Airlines)
            {
                int index = App.ViewModel.CategoryNames.IndexOf(this.category.ToString());
                this.lp_Category.SelectedIndex = index;
            }
            else if (this.category == ProgramCategory.CarRentals)
            {
                this.lp_Category.SelectedItem = "Car Rentals";
            }
          
            

            this.lp_Category.SelectionChanged += new SelectionChangedEventHandler(lp_Category_SelectionChanged);

        }
    }
}